/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package algorithms;

import Jama.EigenvalueDecomposition;


/**
 *
 * @author Thais
 */
public class MDS {
    
    public static double[][] classical(double[][] p, int n){
        double[][] p2 = Matrix.square(p);
        double[][] In = Matrix.identity(n);
        double[][] m11 = Matrix.matrix11(n);
        double[][] p11 = Matrix.mutilply(1.0/n, m11);
        double[][] mJ = Matrix.subtract(In, p11);
        double[][] mB = Matrix.mutilply(-0.5, mJ);
        mB = Matrix.multiply(mB, p2);
        mB = Matrix.multiply(mB, mJ);
        
        Jama.Matrix A = new Jama.Matrix(mB);
         // compute the spectral decomposition
        Jama.EigenvalueDecomposition e = A.eig();
        Jama.Matrix V = e.getV();
        double[] eigenvalues=e.getRealEigenvalues(); 
        double[][] eigenVectors = V.getArray();
        for(int i = 0; i < eigenvalues.length; i++){
            System.out.println(eigenvalues[i] + ";");
        }
        double[] eigen2values = Matrix.extractVector(eigenvalues, 2);
        System.out.println("Eigen values:");
        double[][] eigen2vectors = new double[eigen2values.length][V.getRowDimension()];
        for(int i = 0; i < eigen2values.length; i++){
            System.out.println(eigen2values[i] + ";");
            for (int j = 0; j < eigenvalues.length; j++){
                if (eigen2values[i] == eigenvalues[j]){
                    eigen2vectors[i] = Matrix.getColumn(eigenVectors, j);
                }
            }
        }
        double[][] digonal = Matrix.squaredDigonalMatrix(eigen2values, 2);
        Matrix.print(digonal);
        eigen2vectors = Matrix.transpose(eigen2vectors);
        Matrix.print(eigen2vectors);
        mB = Matrix.multiply(eigen2vectors, digonal);
        Matrix.print(mB);
        return mB;
    }
    
    public static void main(String[] args){
        double[][] B = {{0, 93, 82, 133},{93, 0, 52, 60},
            {82, 52, 0, 111},{133, 60, 111, 0}};
        
        Matrix.print(B);
        classical(B, 4);
    }
    
}
